$filename = "/Users/xp/tmp/a.txt"
$urlstr = "http://localhost:8080/"

$httpClientHandler = New-Object System.Net.Http.HttpClientHandler
Register-ObjectEvent -InputObject $httpClientHandler -EventName ServerCertificateValidationCallback -Action {
    Write-Host $certificate
}

$httpClient = New-Object System.Net.Http.HttpClient($httpClientHandler)

$fileStream = New-Object System.IO.FileStream @($filename, [System.IO.FileMode]::Open)
$streamContent = New-Object System.Net.Http.StreamContent $fileStream

$contentDisposition = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data"
$contentDisposition.Name = "file"
$contentDisposition.FileName = (Split-Path $filename -Leaf)

$streamContent.Headers.ContentDisposition = $contentDispositon

$content = New-Object System.Net.Http.MultipartFormDataContent
$content.Add($streamContent)

$content.Headers.ContentType = "application/octet-stream"
if ($content.TryComputeLength($len)) {
    $content.Headers.ContentLength = $len
}

$uri = New-Object System.Uri($urlstr)

try {
    $response = $httpClient.PostAsync($uri, $content).Result
} catch [Exception] {
    $PSCmdlet.ThrowTerminatingError($_)
} finally {
    if ($null -ne $response) {
        $response.Dispose()
    }
    if ($null -ne $httpClient) {
        $httpClient.Dispose()
    }
}

